<template>
    <a-drawer v-model:visible="visible" width="80%">
        <template #title><div >价格列表</div></template>
        <div v-for="item in list" :key="item.value">
            <a-descriptions style="margin-top: 20px" :data="item" :column="1"/>
        </div>
        <template v-if="loading">
            <a-spin tip="加载中请稍候..."/>
        </template>
        <div>{{errorMsg}}</div>
        <div>{{error}}</div>
    </a-drawer>
</template>
<script setup lang="jsx">
import {watch,inject,ref,nextTick,reactive,computed} from 'vue'
import dictHelp from "@/utils/dicthelp.js";
import shipmentThirdFedex from '@/api/document/shipmentThirdFedex'
import { Message } from '@arco-design/web-vue'

const visible = ref(false)
const error = ref()
const errorMsg = ref()
const loading = ref(true)
const onShow = (id) => {
    visible.value = true
    getPrice(id)
}

const list = ref([
    // [   
    //      {
    //         label: '服务/渠道',
    //         value: 'service',
    //     },
    //     {
    //         label: '基础费用',
    //         value: 'base',
    //     },
    //     {
    //         label: '折扣',
    //         value: 'discounts',
    //     },
    //     {
    //         label: '净费用',
    //         value: 'net',
    //     },
    // ],
])

const getPrice = async (id) => {
    let maxAttempts = 30; // 最大尝试次数
    let attempt = 0;
    list.value = []
    error.value = ''
    errorMsg.value = ''
    loading.value = true
    return new Promise((resolve, reject) => {
        const intervalId = setInterval(async () => {
            if (attempt >= maxAttempts) {
                clearInterval(intervalId);
                reject(new Error("达到最大尝试次数，未能获取价格信息"));
                return;
            }
            
            try {
                const res = await shipmentThirdFedex.read(id);
                // 假设成功的响应有一个特定的标志，例如res.success为true
                if (res.success) {
                    if(res.data.fee.code > 0){
                        clearInterval(intervalId);
                        loading.value = false
                        if(res.data.fee.code == 1){
                            console.log(res.data.fee.data);
                            list.value = res.data.fee.data
                        }else{
                            error.value = res.data.fee.data
                            errorMsg.value = res.data.fee.msg
                        }
                    }
                    console.log("操作成功，定时器已关闭");
                }
            } catch (error) {
                console.error("读取失败:", error);
            }
            attempt++;
        }, 1000); // 每隔1秒执行一次
    });
}

defineExpose({ onShow })
</script>